package cn.com.yinhex.as.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

/**
 * ���ڽ����DOC�������
 */
public class DOCSBuffer
{
	ArrayList list = null;

	HashMap map = null;

	boolean uniq = false;

	/**
	 * ��ʼ��
	 */
	public DOCSBuffer()
	{
		list = new ArrayList();
	}

	public DOCSBuffer(boolean uniq)
	{
		this.uniq = uniq;
		if (uniq)
		{
			map = new HashMap();
		}
		else
		{
			list = new ArrayList();
		}
	}

	/**
	 * ���һ��doc����
	 * 
	 * @param doc
	 */
	public void append(DOC[] docs, String key)
	{
		if (docs == null)
		{
			return;
		}
		for (int i = 0; i < docs.length; i++)
		{
			if (uniq)
			{
				map.put(docs[i].get(key), docs[i]);
			}
			else
			{
				list.add(docs[i]);
			}
		}
	}

	public void append(DOC[] docs)
	{
		if (docs == null)
		{
			return;
		}
		for (int i = 0; i < docs.length; i++)
		{
			list.add(docs[i]);
		}
	}

	/**
	 * ���һ��doc
	 * 
	 * @param doc
	 */
	public void append(DOC doc, String key)
	{
		if (doc == null)
		{
			return;
		}
		if (uniq)
		{
			map.put(doc.get(key), doc);
		}
		else
		{
			list.add(doc);
		}
	}

	public void append(DOC doc)
	{
		if (doc == null)
		{
			return;
		}
		list.add(doc);
	}

	public int size()
	{
		if (uniq)
		{
			return map.size();
		}
		return list.size();
	}

	/**
	 * �ϳ��µ�doc����
	 * 
	 * @return
	 */
	public DOC[] output()
	{
		if (uniq)
		{
			if (map == null || map.size() == 0)
			{
				return null;
			}
			DOC[] docs = new DOC[map.size()];
			Set keyset = map.keySet();
			int i = 0;
			for (Iterator itera = keyset.iterator(); itera.hasNext();)
			{
				String key = (String) itera.next();
				docs[i++] = (DOC) map.get(key);
			}
			return docs;
		}
		else
		{
			if (list == null || list.size() == 0)
			{
				return null;
			}
			DOC[] docs = new DOC[list.size()];
			int i = 0;
			for (Iterator itera = list.iterator(); itera.hasNext();)
			{
				docs[i++] = (DOC) itera.next();
			}
			return docs;
		}
	}
}
